const { Tab, extend } = require('../../dist/index');
var app = getApp();
Page(extend({}, Tab, {
  data: {
    inputShowed: false,
    showResult: false,
    inputVal: "",
    loading: false,
    nodata: false,
    nomore: false,
    couponList: [],
    selectCategory: "0",
    pageIndex: 1,
    sort_method: {
      list: [{
        id: 'hot',
        name: '销量'
      }, {
        id: 'new',
        name: '最新'
      }, {
        id: 'rate',
        name: '折扣'
      }, {
        id: 'price',
        name: '价格'
      }],
      selectedId: 'hot'
    },
  },
  onLoad: function (res) {
    var that = this;
    
    that.setData({
      inputVal: res.value,
      cate_method: wx.getStorageSync("menu_cate")
    });
    this.getMoreCouponList();
  }
  ,
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },
  searchData: function (e) {
    this.setData({
      nodata: false,
      inputVal: e.detail.value,
      showResult: true,
      couponList: []
    })
    this.getMoreCouponList();
  },
  //取商品
  getMoreCouponList: function () {
    wx.showLoading({
      title: '搜索中...',
    });
    var that = this
    wx.request({
      url: app.globalData.apiurl + "/Home/Item/getItems",
      dataType: 'json',
      data: {
        //"type": that.data.types,
        "so": that.data.inputVal,
        "sort": that.data.sorts,
        "p": that.data.pageIndex,
        "size": 10,
        "cid": that.data.selectCategory
      },
      success: function (resRequest) {
        // console.log(resRequest.data.data.result);      
        if (resRequest.statusCode === 200) {
          if (resRequest.data.status == "0") {
            that.setData({
              couponList: that.data.couponList.concat(resRequest.data.data.items_list),
              isLoading: false
            })
          } else {
            that.setData({
              isLoading: true,
              loadOver: true
            })
          }
        };
        wx.hideLoading();
      }
    });
  },
  handleZanTabChange(e) {
    var componentId = e.componentId;
    var selectedId = e.selectedId;
    if (componentId === "cate_method") {
      this.setData({
        selectCategory: selectedId
      })
    }
    if (componentId === "sort_method") {
      this.setData({
        sorts: selectedId
      })
    }
    this.setData({
      [`${componentId}.selectedId`]: selectedId,
      isLoading: true,
      couponList: [],
      pageIndex: 1,
    });
    this.getMoreCouponList()
  },
  onPullDownRefresh: function () {
    this.setData({
      couponList: [],
      isLoading: true,
      pageIndex: 1
    })
    wx.stopPullDownRefresh()
    this.getMoreCouponList()
  },
  onReachBottom: function () {
    this.setData({
      isLoading: true,
      pageIndex: this.data.pageIndex + 1
    })
    this.getMoreCouponList()
  },



}));
